var GameData = require('../module/gameData')();
cc.Class({
    extends: cc.Component,

    properties: {
        sp1: cc.Sprite,
        contet: cc.Node,
        coinNum: cc.Label,
        cost: cc.Label,
        title: cc.Label,
        addNum:cc.Label,
        nowNum:cc.Label,
        btn1: cc.Sprite,

    },

    onLoad() {
        this.fishData = GameData.getlocalData("fishData");
        this.title.string = GameData.gameText.luck.text1;
        this.coinNum.string = this.fishData.coinNum + "";
        this.updatetext();
        this.updatetext();
    },
    updatetext() {
        this.fishData = GameData.getlocalData("fishData");
        this._level = this.fishData.upgrade.up_lucky;
        this.nextLevelCost;
        var addNum;
        if (this._level > 9) {
            this.nextLevelCost = "MAX";
            addNum =  "";
            // this.node.getChildByName("right").getChildByName("down").active=false;
        } else {
            // this.node.getChildByName("right").getChildByName("down").active=true;
            var _costNum = GameData.upgradeData.luck[this._level].price;
            if (this.fishData.coinNum >= _costNum) {
            }else {
                this.cost.node.parent.getComponent(cc.Sprite).spriteFrame=this.btn1.getComponent(cc.Sprite).spriteFrame;
            }
            this.nextLevelCost = GameData.upgradeData.luck[this._level].price;
            addNum = "+" + parseInt((GameData.upgradeData.luck[this._level].gailv - GameData.upgradeData.luck[this._level - 1].gailv)) + "%";
        }

        this.cost.string = this.nextLevelCost + "";
        this.nowNum.string = parseInt(GameData.upgradeData.luck[this._level - 1].gailv) + "%";
        this.addNum.string = addNum;
        for (var a = 0; a < this._level; a++) {
            this.contet.getChildByName("sp" + (a + 1)).getComponent(cc.Sprite).spriteFrame = this.sp1.getComponent(cc.Sprite).spriteFrame;
        }
    },
    selectBtn(evt) {
        let _name = evt.currentTarget.name;
        switch (_name) {
            case "upgrade":
                if (this._level <= 9) {
                    this.fishData = GameData.getlocalData("fishData");
                    var _costNum = GameData.upgradeData.luck[this._level].price;
                    if (this.fishData.coinNum >= _costNum) {
                        this.coinNum.node.parent.getComponent("gamecoin").coinAddEffect(this.fishData.coinNum, this.fishData.coinNum - _costNum);
                        this.fishData.coinNum -= this.nextLevelCost;
                        this.fishData.upgrade.up_lucky++;
                        GameData.savelocalData(this.fishData);
                        this.updatetext()
                    }
                }
                break;
            case "goback":
                if (GameData.gamhomeshow) {

                }
                cc.director.preloadScene("Home", function () {
                    cc.director.loadScene("Home");
                });
                break;
        }
    },
    start() {

    },

    // update (dt) {},
});
